package demoMod.nanaki.characters;

import basemod.ReflectionHacks;
import basemod.abstracts.CustomPlayer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.math.MathUtils;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.EnergyManager;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.city.Vampires;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.Hitbox;
import com.megacrit.cardcrawl.helpers.ModHelper;
import com.megacrit.cardcrawl.helpers.ScreenShake;
import com.megacrit.cardcrawl.localization.CharacterStrings;
import com.megacrit.cardcrawl.orbs.AbstractOrb;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.rooms.MonsterRoom;
import com.megacrit.cardcrawl.rooms.RestRoom;
import com.megacrit.cardcrawl.screens.CharSelectInfo;
import demoMod.anm2player.AnimatedActor;
import demoMod.nanaki.NanakiMod;
import demoMod.nanaki.animations.GifAnimation;
import demoMod.nanaki.cards.nanaki.ActiveHymn;
import demoMod.nanaki.cards.nanaki.Defend_N;
import demoMod.nanaki.cards.nanaki.Mesmerizing;
import demoMod.nanaki.cards.nanaki.Strike_N;
import demoMod.nanaki.patches.AbstractCardEnum;
import demoMod.nanaki.patches.AbstractPlayerEnum;
import demoMod.nanaki.patches.CharacterSelectScreenPatch;
import demoMod.nanaki.relics.TheBell;
import demoMod.nanaki.stances.AbstractStrophe;
import demoMod.nanaki.ui.orb.NanakiEnergyOrb;

import java.util.ArrayList;

public class NanakiCharacter extends CustomPlayer {
    private static final CharacterStrings charStrings;

    public static final String NAME;

    public static final String DESCRIPTION;
    private static final String SHOULDER_2 = NanakiMod.getResourcePath("char/shoulder2.png");
    private static final String SHOULDER_1 = NanakiMod.getResourcePath("char/shoulder1.png");
    private static final String CORPSE = NanakiMod.getResourcePath("char/fallen.png");
    private static final String STAND = NanakiMod.getResourcePath("char/Test3.png");

    private GifAnimation gifAnimation;
    private FrameBuffer frameBuffer;

    public NanakiCharacter(String name) {
        super(name, AbstractPlayerEnum.NANAKI, new NanakiEnergyOrb(), null, null);
        this.dialogX = this.drawX + 0.0F * Settings.scale;
        this.dialogY = this.drawY + 250.0F * Settings.scale;

        initializeClass(STAND, SHOULDER_2, SHOULDER_1, CORPSE,
                getLoadout(),
                0.0F, 5.0F, 240.0F, 300.0F,
                new EnergyManager(3));
        if (ModHelper.enabledMods.size() > 0 && (ModHelper.isModEnabled("Diverse") || ModHelper.isModEnabled("Chimera") || ModHelper.isModEnabled("Blue Cards"))) {
            this.masterMaxOrbs = 1;
        }

        //预加载gif动画，防止首次加载时卡顿
        new GifAnimation(NanakiMod.getResourcePath("char/game_killer.gif"));

        switch (CharacterSelectScreenPatch.reskinIndex) {
            case 0: //原皮
            default:
                break;
            case 1: //游戏杀手
                this.gifAnimation = new GifAnimation(NanakiMod.getResourcePath("char/game_killer.gif"));
                this.hb_w = 200.0F * Settings.scale;
                this.hb_h = 288.8F * Settings.scale;
                this.hb = new Hitbox(this.hb_w, this.hb_h);
                this.healthHb = new Hitbox(this.hb.width, 72.0F * Settings.scale);
                this.refreshHitboxLocation();
                this.frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
                this.img = frameBuffer.getColorBufferTexture();
                break;
        }
    }

    @Override
    public void render(SpriteBatch sb) {
        this.stance.render(sb);
        if ((AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT || AbstractDungeon.getCurrRoom() instanceof MonsterRoom) && !this.isDead) {
            this.renderHealth(sb);
            for (AbstractOrb o : this.orbs) {
                o.render(sb);
            }
        }
        if (!(AbstractDungeon.getCurrRoom() instanceof RestRoom)) {
            if (!(boolean) ReflectionHacks.getPrivate(this, AbstractPlayer.class, "renderCorpse")) {
                if (this.gifAnimation == null) {
                    this.renderPlayerImage(sb);
                } else {
                    sb.end();
                    frameBuffer.begin();
                    sb.begin();
                    Gdx.gl.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
                    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                    sb.setColor(1.0F, 1.0F, 1.0F, 1.0F);
                    this.gifAnimation.render(sb, this.drawX - this.hb_w * 0.5625F + this.animX, this.drawY + this.animY + AbstractDungeon.sceneOffsetY - 24.0F * Settings.scale, this.hb_w, this.hb_h);
                    sb.end();
                    frameBuffer.end();
                    sb.begin();

                    sb.setColor(Color.WHITE);
                    sb.draw(this.img, !this.flipHorizontal ? (this.drawX + this.animX) * 2.0F - this.img.getWidth() : 0, this.flipVertical ? (this.drawY + this.animY + AbstractDungeon.sceneOffsetY - 24.0F * Settings.scale) * 2.0F - this.img.getHeight() : 0,
                            this.img.getWidth(), this.img.getHeight(),
                            0, 0,
                            this.img.getWidth(), this.img.getHeight(),
                            !this.flipHorizontal, !this.flipVertical);
                }
            } else {
                sb.setColor(Color.WHITE);
                sb.draw(this.img, this.drawX - (float)this.img.getWidth() * Settings.scale / 2.0F + this.animX, this.drawY, (float)this.img.getWidth() * Settings.scale, (float)this.img.getHeight() * Settings.scale, 0, 0, this.img.getWidth(), this.img.getHeight(), this.flipHorizontal, this.flipVertical);
            }

            this.hb.render(sb);
            this.healthHb.render(sb);
        } else {
            this.renderShoulderImg(sb);
        }
//        for (AnimatedActor actor : AbstractStrophe.vfxList) {
//            actor.render(sb);
//        }
    }

    @Override
    public ArrayList<String> getStartingDeck() {
        ArrayList<String> ret = new ArrayList<>();
        ret.add(Strike_N.ID);
        ret.add(Strike_N.ID);
        ret.add(Strike_N.ID);
        ret.add(Strike_N.ID);
        ret.add(Defend_N.ID);
        ret.add(Defend_N.ID);
        ret.add(Defend_N.ID);
        ret.add(Defend_N.ID);
        ret.add(ActiveHymn.ID);
        ret.add(Mesmerizing.ID);
        return ret;
    }

    @Override
    public ArrayList<String> getStartingRelics() {
        ArrayList<String> retVal = new ArrayList<>();
        retVal.add(TheBell.ID);
        return retVal;
    }

    @Override
    public CharSelectInfo getLoadout() {
        return new CharSelectInfo(NAME, DESCRIPTION, 70, 70, 0, 99, 5, this, getStartingRelics(), getStartingDeck(), false);
    }

    @Override
    public String getTitle(PlayerClass playerClass) {
        return NAME;
    }

    @Override
    public AbstractCard.CardColor getCardColor() {
        return AbstractCardEnum.NANAKI;
    }

    @Override
    public Color getCardRenderColor() {
        return NanakiMod.GREYN.cpy();
    }

    @Override
    public AbstractCard getStartCardForEvent() {
        return new Strike_N();
    }

    @Override
    public Color getCardTrailColor() {
        return NanakiMod.GREYN.cpy();
    }

    @Override
    public int getAscensionMaxHPLoss() {
        return 7;
    }

    @Override
    public BitmapFont getEnergyNumFont() {
        return FontHelper.energyNumFontBlue;
    }

    @Override
    public void doCharSelectScreenSelectEffect() {
        CardCrawlGame.sound.playA("ATTACK_DAGGER_2", MathUtils.random(-0.2F, 0.2F));
        CardCrawlGame.screenShake.shake(ScreenShake.ShakeIntensity.MED, ScreenShake.ShakeDur.SHORT, false);
    }

    @Override
    public String getCustomModeCharacterButtonSoundKey() {
        return "ATTACK_DAGGER_2";
    }

    @Override
    public String getLocalizedCharacterName() {
        return NAME;
    }

    @Override
    public AbstractPlayer newInstance() {
        return new NanakiCharacter(NAME);
    }

    @Override
    public String getSpireHeartText() {
        return charStrings.TEXT[1];
    }

    @Override
    public Color getSlashAttackColor() {
        return NanakiMod.GREYN.cpy();
    }

    @Override
    public AbstractGameAction.AttackEffect[] getSpireHeartSlashEffect() {
        return new AbstractGameAction.AttackEffect[]{AbstractGameAction.AttackEffect.SLASH_HEAVY, AbstractGameAction.AttackEffect.FIRE, AbstractGameAction.AttackEffect.SLASH_DIAGONAL, AbstractGameAction.AttackEffect.SLASH_HEAVY, AbstractGameAction.AttackEffect.FIRE, AbstractGameAction.AttackEffect.SLASH_DIAGONAL};
    }

    @Override
    public String getVampireText() {
        return Vampires.DESCRIPTIONS[1];
    }

    static {
        charStrings = CardCrawlGame.languagePack.getCharacterString(NanakiMod.makeID("NanakiCharacter"));
        NAME = charStrings.NAMES[0];
        DESCRIPTION = charStrings.TEXT[0];
    }
}
